/**
 * @author wangxiaowei
 * 岗位查询
 */
var sort = null; 
var grid = null;
function initpostlist(){
	 Ext.BLANK_IMAGE_URL=Ext.get('basePath').getValue()+"ajaxFramework/ext-2.1/resources/images/default/s.gif";	
	var colModel = new Ext.grid.ColumnModel([
		{header:'岗位名称',dataIndex:'postName',menuDisabled : true},
		{header:'描述',dataIndex:'postDescription',menuDisabled : true},		
		{header:'状态',dataIndex:'start',menuDisabled : true},
		{header:'操作',dataIndex:'operate',menuDisabled : true,renderer:function(s,m,rec){
		if(rec.data.start == '禁用'){
		return "<a  href="+"javascript:enablePost('"+rec.data.postId+"')>启用</a>  <a  href="+"javascript:updateToPost('"+rec.data.postId+"','"+rec.data.first+"')>修改</a>";
		}else{
		return "<a  href="+"javascript:enablePost('"+rec.data.postId+"')>禁用</a>  <a  href="+"javascript:updateToPost('"+rec.data.postId+"','"+rec.data.first+"')>修改</a>";
		}
		}},
		{header:'postId',dataIndex:'postId',hidden:true}
	]);
	var url = Ext.get('url').getValue();
	sort = new Ext.data.JsonStore({
		url:url,
		fields:['postName','postDescription','start','operate','postId','first'],
		root:'root',
		totalProperty:'totalProperty'
	});
	var bbar = new Ext.PagingToolbar({
		pageSize:15,
		store:sort,
		displayInfo:true,
		emptyMsg:'没有记录'
	});
	
    grid = new Ext.grid.GridPanel({
		title:'岗位列表',
		cm:colModel,
		renderTo:'topic-grid',
		store:sort,
		loadMask:true,
		autoHeight:true,
		tbar:[{
			 text: '添加',
             iconCls: 'silk-add',
              handler:function(){
             	window.location.href=Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/addToPostForm.do';
             }
		}],
		bbar:bbar,
		viewConfig:{
			forceFit:true,
			enableRowBody:true,
			getRowClass:function(record,rowIndex,p,ds){
				if(record.get('start') == '禁用')
				{
					return 'red-row';
				}
			}
		}
	});
	sort.load({params:{start:parseInt(Ext.get('postliststart').getValue()), limit: 15}});	
}
/**
 * 查询岗位下的用户生成树放到WINDOW中
 * @param {} deptid
 */
function selectpostuser(postid){
	 var url = Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/userByPost.do?postid='+postid;
	 var load = new Ext.tree.TreeLoader({
	 	  dataUrl:url
	 });
	 var root = new Ext.tree.AsyncTreeNode({
	 	id:'0',
	 	text:'天津利达粮油有限公司'
	 })
	 var tree = new Ext.tree.TreePanel({
	 		x:5,
	 		y:5,
        	autoScroll: true,
        	animate: true,
        	containerScroll: true,
        	border: true,
        	frame:true,
	        loader:load,
	        height:467,
	        width:505
	    });
	tree.setRootNode(root); 
	var win = new Ext.Window({
		title:'人员列表',
		width:520,
		height:500,
		modal:true,
		frame:true,
		closeAction:'hide',
		items:tree
	});
	win.show();
}

function enablePost(id){
	 Ext.MessageBox.show({
          title:'消息',
          msg: '是否执行此操作?',
          buttons: Ext.MessageBox.YESNO,
          fn: function(btn){
          	  if(btn == 'yes'){
          	  	    var url = Ext.get('urlenable').getValue();
				Ext.Ajax.request({
					url:url,
					params:{
						id:id
					},
					success:function(response, options){
						    var responseArray = Ext.util.JSON.decode(response.responseText);                                              
		                     if(responseArray.success==true){
		                        Ext.Msg.alert('信息','操作成功！',function(){
		                        	sort.reload({callback :function(){
			                        		grid.getView().refresh();	
			                        	}});
		                        });     
		                     }
		                    else{
		                    	Ext.Msg.alert('信息','操作失败！');     
		                    } 
					}
				});
				
          	  }	
          },
          icon: Ext.MessageBox.QUESTION
      });		
}

function updateToPost(id,first){
	 window.location.href=Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/updateToPost.do?postId='+id+'&first='+first;	
}
